package com.kaifamiao.datastreams;

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

public class DataOutputTest {

    public static void main(String[] args) throws IOException {

        final byte[] nameBytes = new byte[30];

        OutputStream out = new FileOutputStream("afang.data");
        DataOutputStream dos = new DataOutputStream(out);

        int id = 1001;
        String name = "李小芳";
        byte[] src = name.getBytes(StandardCharsets.UTF_8);
        System.arraycopy(src, 0, nameBytes, 0, src.length );
        char gender = '女';
        boolean married = false;
        short year = 2001;
        byte month = 12;
        byte date = 12;

        // 把指定int数值对应的四个字节写入到字节输出流中
        dos.writeInt( id );
        // 将指定数组中所有的字节写入到字节输出流中
        dos.write( nameBytes );
        // 将指定char值对应的两个字节写入到字节输出流中
        dos.writeChar( gender );
        // 将一个字节写入到字节输出流中
        dos.writeBoolean( married );
        dos.writeShort( year );
        dos.writeByte( month ); // dos.write( month )
        dos.writeByte( date ); // dos.write( date )

        dos.close();
        out.close();

    }

}
